﻿using System.Text;

namespace CodeParser.Tokens
{
    public class Keychars
        : TokenParser
    {

        public Keychars(TokenType type, params char[] allowedChars)
            : base(type)
        {
            Chars = allowedChars;
        }

        public override Token GetTokenAt(string source, int index)
        {
            var currentChar = source[index];
            if (index < source.Length - 1)
            {
                var nextChar = source[index + 1];
                for (var j = 0; j < _charCount; j++)
                {
                    if (nextChar == _chars[j])
                    {
                        return null;
                    }
                }
            }
            for (var j = 0; j < _charCount; j++)
            {
                if (currentChar == _chars[j])
                {

                    return new Token() { Index = index, Type = Type.Value, Value = "" + currentChar };
                }
            }
            return null;
        }

        private int _charCount;
        private char[] _chars;
        public char[] Chars
        {
            get { return _chars; }
            private set
            {
                _chars = value;
                _charCount = value.Length;
            }
        }


    }
}
